<?php

namespace Apps\AppApi\Method\BeautyParlor;
use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Libs\PicUrl;
use Apps\Common\Models\BeautyParlorComment;


/**
 * 评论列表
 * @package Apps\AppApi\Method\BeautyParlor
 */
class CommentList extends BaseMethod
{
    /**
     * @var \Apps\Common\Models\BeautyParlorComment array
     */
    private $comments = [];

    public function run()
    {
        $bp_id = intval($this->getDataItem('id',0));
        $page = intval($this->getDataItem('page',1));

        $limit = $this->getConfig()->limit;
        $offset = ($page-1)*$limit;

        if(0 < $bp_id) {
            $this->comments = BeautyParlorComment::query()
                ->columns(['u.user_id',
                    'u.user_nickname',
                    'u.user_cover',
                    'u.user_expert',
                    'u.user_gender',
                    'Apps\Common\Models\BeautyParlorComment.comment_id as comment_id',
                    'Apps\Common\Models\BeautyParlorComment.comment_info as comment_info',
                    'Apps\Common\Models\BeautyParlorComment.comment_photos as comment_photos',
                    'Apps\Common\Models\BeautyParlorComment.comment_floor as comment_floor',
                    'Apps\Common\Models\BeautyParlorComment.comment_addtime as addtime'])
                ->leftJoin('Apps\Common\Models\UserBase',
                    'Apps\Common\Models\BeautyParlorComment.user_id=u.user_id','u')
                //->where('beauty_parlor_id='.$bp_id)//原代码
                ->where('Apps\Common\Models\BeautyParlorComment.beauty_parlor_id='.$bp_id.' and Apps\Common\Models\BeautyParlorComment.comment_state = 1')//修改处
                ->orderBy('Apps\Common\Models\BeautyParlorComment.comment_id desc')
                ->limit($limit,$offset)
                ->execute()->toArray();
        }
        $this->comments = $this->comments ? $this->comments : [];
        $data = [];
        foreach($this->comments as $arr) {
            $arr['user_cover'] = PicUrl::UserCover($arr['user_cover'], $this->getDi());
            $arr['comment_photos'] = PicUrl::BeautyParlorComment(json_decode($arr['comment_photos']), $this->getDi());
            if(unserialize(base64_decode($arr['comment_info']))){
                $arr['comment_info'] = unserialize(base64_decode($arr['comment_info']));
            }
            $data[] = $arr;
        }
        $this->setResult($data);
    }

}